package com.zhou.scaffolding.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.io.Serializable;

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

  /**
   * 是否成功
   */
  private Boolean succ;


  /**
   * 成功数据
   */
  @JsonInclude(JsonInclude.Include.NON_NULL)
  private T data;

  /**
   * 错误码
   */
  private String code;

  /**
   * 错误描述
   */
  private String msg;


  public static Result data(Object data) {
    Result success = success(ErrorCode.SUCCESS);
    success.setData(data);
    return success;
  }

  public static Result success(ErrorCode resultEnum) {
    Result result = new Result();
    result.succ = resultEnum.isSuccess();
    result.code = resultEnum.getCode();
    result.msg = resultEnum.getMessage();
    return result;
  }

  public static Result success() {
    return success(ErrorCode.SUCCESS);
  }

  public static Result fail(){
      return success(ErrorCode.FAIL);
  }
}